<template>
  <!--    <avue-crud :option="option"-->
  <!--               :table-loading="loading"-->
  <!--               :data="data"-->
  <!--               :page="page"-->
  <!--               :permission="permissionList"-->
  <!--               :before-open="beforeOpen"-->
  <!--               v-model="form"-->
  <!--               ref="crud"-->
  <!--               @row-update="rowUpdate"-->
  <!--               @row-save="rowSave"-->
  <!--               @row-del="rowDel"-->
  <!--               @search-change="searchChange"-->
  <!--               @search-reset="searchReset"-->
  <!--               @selection-change="selectionChange"-->
  <!--               @current-change="currentChange"-->
  <!--               @size-change="sizeChange"-->
  <!--               @on-load="onLoad">-->
  <!--      <template slot="menuLeft">-->
  <!--        <el-button type="danger"-->
  <!--                   size="small"-->
  <!--                   icon="el-icon-delete"-->
  <!--                   plain-->
  <!--                   v-if="permission.devicetable_delete"-->
  <!--                   @click="handleDelete">删 除-->
  <!--        </el-button>-->
  <!--      </template>-->
  <!--    </avue-crud>-->

  <div class="container flex-column">
    <div class="tool-box  flex-between">
      <div class="left flex-box">
        <div class="flex-box">
          <span>场地名称</span>
          <el-input type="text" v-model="query.siteName"></el-input>
        </div>

      </div>
      <div class="right flex-box">
        <el-button plain icon="el-icon-search" @click="page.currentPage = 1;onLoad(page)">搜索</el-button>
        <el-button plain  type="danger" icon="el-icon-delete" @click="searchReset">清空</el-button>
      </div>

    </div>
    <div class="m-table">
      <div class="btn-box flex-between">
        <el-button  type="primary" plain icon="el-icon-circle-plus-outline"
                    v-if="permissionList.addBtn" @click="addClick">新增</el-button>
      </div>
      <el-table header-cell-class-name="table-header"
                v-loading="loading"
                :data="data" stripe
                style="width: 100%">
        <el-table-column
          type="index"
          width="80"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          prop="customName"
          width="130"
          label="客户人员名称"
        >
        </el-table-column>
        <el-table-column
          prop="siteName"
          label="场地名称"
        >
        </el-table-column>
        <el-table-column
          prop="experimentName"
          label="实验名称"
        >
        </el-table-column>
        <el-table-column
          prop="experimentUserName"
          width="120"
          label="实验人员名称"
        >
        </el-table-column>
        <el-table-column
          prop="experimentUserPhone"
          width="130"
          label="实验人员手机号"
        >
        </el-table-column>
        <el-table-column
          prop="experimentUserPhone"
          width="90"
          label="实验状态"
        >
          <template slot-scope="{row}">{{row.status.msg}}</template>
        </el-table-column>
        <el-table-column
          prop="experimentStartTime"
          label="实验开始时间"
        >
        </el-table-column>
        <el-table-column
          prop="experimentEndTime"
          label="实验结束时间"
        >

        </el-table-column>
<!--        <el-table-column-->
<!--          prop="cameraDevices"-->
<!--          label="实验摄像头"-->
<!--        >-->
<!--          <template slot-scope="{row}">-->
<!--            {{row.cameraDevices.map(o=>{-->
<!--            return o.deviceName-->
<!--            }).toString()}}-->
<!--          </template>-->
<!--        </el-table-column>-->

        <el-table-column
          v-if="hasOperatePermission"
          prop="date"
          label="操作"
        >
          <template slot-scope="{row}">
            <div class="flex-box">
              <el-button v-if="permissionList.viewBtn" size="mini"
                         @click="viewClick(row)"
                         type="primary" plain>查看</el-button>
              <el-button v-if="permissionList.editBtn" size="mini"
                         type="primary"
                         :disabled="row.status.code!==1"
                         @click="editClick(row)"
                         plain>编辑</el-button>
              <el-button v-if="permissionList.delBtn" size="mini"
                         @click="rowDel(row)"
                         :disabled="row.status.code!==1"
                         type="danger" plain>删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @current-change="currentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          layout="total,  prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>
    <add-modal ref="add" @rowSave="rowSave" @rowUpdate="rowUpdate" @success="page.currentPage = 1;onLoad(page)" :deviceTypeList="deviceTypeList"></add-modal>
  </div>



</template>

<script>
  import {getList,add, update, remove,} from "../../api/platform/workshopmanagetable";
  import {mapGetters} from "vuex";
  import addModal from './experiment-components/add-modal'
  import _ from 'lodash'
  export default {
    components:{
      addModal
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        deviceTypeList:[],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "主键",
              prop: "id",
              addDisplay: false,
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }]
            },
            {
              label: "设备id",
              prop: "deviceId",
              rules: [{
                required: true,
                message: "请输入设备id",
                trigger: "blur"
              }]
            },
            {
              label: "设备名称",
              prop: "deviceName",
              rules: [{
                required: true,
                message: "请输入设备名称",
                trigger: "blur"
              }]
            },
            {
              label: "产品编号",
              prop: "productId",
              rules: [{
                required: true,
                message: "请输入产品编号",
                trigger: "blur"
              }]
            },
            {
              label: "产品名称",
              prop: "productName",
              rules: [{
                required: true,
                message: "请输入产品名称",
                trigger: "blur"
              }]
            },
            {
              label: "连接实例:mqtt,udp,串口,http",
              prop: "connectionInstance",
              rules: [{
                required: true,
                message: "请输入连接实例:mqtt,udp,串口,http",
                trigger: "blur"
              }]
            },
            {
              label: "ip",
              prop: "ipAddress",
              rules: [{
                required: true,
                message: "请输入ip",
                trigger: "blur"
              }]
            },
            {
              label: "端口",
              prop: "portValue",
              rules: [{
                required: true,
                message: "请输入端口",
                trigger: "blur"
              }]
            },
            {
              label: "远端地址",
              prop: "urlAddress",
              rules: [{
                required: true,
                message: "请输入远端地址",
                trigger: "blur"
              }]
            },
            {
              label: "账户",
              prop: "mqttUserName",
              rules: [{
                required: true,
                message: "请输入账户",
                trigger: "blur"
              }]
            },
            {
              label: "密码",
              prop: "mqttPwd",
              rules: [{
                required: true,
                message: "请输入密码",
                trigger: "blur"
              }]
            },
            {
              label: "连接通道ID",
              prop: "channelId",
              addDisplay: false,
              rules: [{
                required: true,
                message: "请输入连接通道ID",
                trigger: "blur"
              }]
            },
            {
              label: "连接状态1在线,2:离线,3未连接",
              prop: "connectionState",
              addDisplay: false,
              rules: [{
                required: true,
                message: "请输入连接状态1在线,2:离线,3未连接",
                trigger: "blur"
              }]
            },
            {
              label: "离线时间",
              prop: "offlineTime",
              addDisplay: false,
              rules: [{
                required: true,
                message: "请输入离线时间",
                trigger: "blur"
              }]
            },
            {
              label: "省",
              prop: "provinceCode",
              rules: [{
                required: true,
                message: "请输入省",
                trigger: "blur"
              }]
            },
            {
              label: "省",
              prop: "provinceName",
              rules: [{
                required: true,
                message: "请输入省",
                trigger: "blur"
              }]
            },
            {
              label: "市",
              prop: "cityCode",
              rules: [{
                required: true,
                message: "请输入市",
                trigger: "blur"
              }]
            },
            {
              label: "市",
              prop: "cityName",
              rules: [{
                required: true,
                message: "请输入市",
                trigger: "blur"
              }]
            },
            {
              label: "县",
              prop: "countyCode",
              rules: [{
                required: true,
                message: "请输入县",
                trigger: "blur"
              }]
            },
            {
              label: "县",
              prop: "countyName",
              rules: [{
                required: true,
                message: "请输入县",
                trigger: "blur"
              }]
            },
            {
              label: "详细地址",
              prop: "detailedlyAddress",
              rules: [{
                required: true,
                message: "请输入详细地址",
                trigger: "blur"
              }]
            },
            {
              label: "精度",
              prop: "precision",
              rules: [{
                required: true,
                message: "请输入精度",
                trigger: "blur"
              }]
            },
            // {
            //   label: "维度",
            //   prop: "dimension",
            //   rules: [{
            //     required: true,
            //     message: "请输入维度",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "设备状态1启用,2:停用,",
              prop: "deviceState",
              addDisplay: false,
              rules: [{
                required: true,
                message: "请输入设备状态1启用,2:停用,",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remark",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.workshopmanagetable_add, false),
          viewBtn: this.vaildData(this.permission.workshopmanagetable_view, false),
          delBtn: this.vaildData(this.permission.workshopmanagetable_delete, false),
          editBtn: this.vaildData(this.permission.workshopmanagetable_edit, false)
        };
      },
      hasOperatePermission(){
        return this.permissionList.viewBtn || this.permissionList.delBtn || this.permissionList.editBtn
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      deviceTypeChange(val){
        const node = this.deviceTypeList.find(o=>{
          return o.id === val
        })
        if(node){
          this.query.deviceTypeName = node.attributeName
        }
      },
      getDevType(){
        getDevType().then(res=>{
          const data = res.data.data;
          this.deviceTypeList = data
        })
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      addClick(){
        this.$refs.add.type = 'add';
        this.$refs.add.showModal()
      },
      editClick(row){
        this.$refs.add.type = 'edit';
        this.$refs.add.form = _.cloneDeep(row)
        this.$refs.add.form.daterange = [row.experimentStartTime,row.experimentEndTime]
        this.$refs.add.showModal()

      },
      viewClick(row){
        this.$refs.add.type = 'view';
        this.$refs.add.form = _.cloneDeep(row)
        this.$refs.add.form.daterange = [row.experimentStartTime,row.experimentEndTime]
        this.$refs.add.showModal()
      },

      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
        this.onLoad(this.page)
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    },
    mounted() {
      this.onLoad(this.page)
    }
  };
</script>

<style>
</style>
